#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;
#define NUM 3
int tickets = 1000;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

class ThreadData
{
public:
    std::string name_;
    pthread_t tid_;
};

void *GetTickets(void *arg)
{
    ThreadData *thread = static_cast<ThreadData *>(arg);
    while (1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        cout << thread->name_ << " 正在抢票" << endl;
        pthread_mutex_unlock(&mutex);
    }
    return nullptr;
}

int main()
{
    // pthread_mutex_init(&mutex,nullptr);
    // pthread_cond_init(&cond,nullptr);
    for (int i = 0; i < NUM; i++)
    {
        ThreadData *thread = new ThreadData();
        thread->name_ = "Thread" + to_string(i);
        pthread_create(&thread->tid_, nullptr, GetTickets, (void *)thread);
    }
    while (1)
    {
        sleep(1);
        pthread_cond_signal(&cond);
    }
}